#' reading in:
#'   data_for_analysis.csv  (written out by 4-mreging.R)
#' conducting all analyses
#' writing out: 
#'   

wcy = read_csv('data_for_analysis.csv')


#### MINDEF ANALYSIS ####

# Ministers of Defense

mindef_table = wcy |>
  filter(!is.na(mindef_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         # if you want to restrict to democracies:
         # & dem ==1
  ) |>
  group_by(leader_hawkdove ) |> 
  summarise(
    mindef_H = mean(mindef_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mindef_D = mean(mindef_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mindef_indep = mean(mindef_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mindef_sameparty = mean(mindef_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

mindef_table_flipped = mindef_table[c(2,1,3),] |> add_column(NA_character_, .before=1) |> t() 

# Ministers of Defense, by election proximity

elec_mindef_table = wcy |>
  filter(!is.na(mindef_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl ==0 
         # if you want to restrict to democracies:
         # & dem ==1
  ) |>
  group_by(leader_hawkdove, elec2) |> 
  summarise(
    mindef_H = mean(mindef_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mindef_D = mean(mindef_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mindef_indep = mean(mindef_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mindef_sameparty = mean(mindef_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

elec_mindef_table_flipped = elec_mindef_table[c(3,4,1,2,5,6),] |> t()

# writing out full MinDef table

cbind(mindef_table_flipped, elec_mindef_table_flipped) |> 
  xtable() |> 
  print(file='tables/mindef_table.tex')


# mindef, only presidential systems

mindef_pres_table = wcy |>
  filter(!is.na(mindef_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         & leader_pm ==0
  ) |>
  group_by(leader_hawkdove ) |> 
  summarise(
    mindef_H = mean(mindef_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mindef_D = mean(mindef_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mindef_indep = mean(mindef_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mindef_sameparty = mean(mindef_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

mindef_pres_table_flipped = mindef_pres_table[c(2,1,3),]   |> t() 


# mindef, only parliamentary systems

mindef_parl_table = wcy |>
  filter(!is.na(mindef_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         & leader_pm ==1
  ) |>
  group_by(leader_hawkdove ) |> 
  summarise(
    mindef_H = mean(mindef_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mindef_D = mean(mindef_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mindef_indep = mean(mindef_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mindef_sameparty = mean(mindef_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

mindef_parl_table_flipped = mindef_parl_table[c(2,1,3),]  |> t() 



# writing out MinDef Pres/Parl table

cbind(mindef_pres_table_flipped, mindef_parl_table_flipped) |> 
  xtable() |>
  print(file='tables/mindef_system_table.tex')



#### MINFORAFF ANALYSIS ####

mfa_table = wcy |>
  filter(!is.na(mfa_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         # if you want to restrict to democracies:
         #  & dem ==1
  ) |>
  group_by(leader_hawkdove ) |> 
  summarise(
    mfa_H = mean(mfa_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mfa_D = mean(mfa_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mfa_indep = mean(mfa_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mfa_sameparty = mean(mfa_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

mfa_table_flipped = mfa_table[c(2,1,3),] |> add_column(NA_character_, .before=1) |> t() 


# Ministers of Foreign Affairs, by election proximity

elec_mfa_table = wcy |>
  filter(!is.na(mfa_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         # if you want to restrict to democracies:
         # & dem ==1
  ) |>
  group_by(leader_hawkdove , elec2) |> 
  summarise(
    mfa_H = mean(mfa_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mfa_D = mean(mfa_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mfa_indep = mean(mfa_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mfa_sameparty = mean(mfa_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )


elec_mfa_table_flipped = elec_mfa_table[c(3,4,1,2,5,6),] |> t()

# writing out full MinForAff table


cbind(mfa_table_flipped, elec_mfa_table_flipped) |> 
  xtable() |> 
  print(file='tables/mfa_table.tex')




# Ministers of Foreign Affairs, presidential system only

mfa_pres_table = wcy |>
  filter(!is.na(mfa_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         # if you want to restrict to democracies:
         #  & dem ==1
         & leader_pm==0
  ) |>
  group_by(leader_hawkdove ) |> 
  summarise(
    mfa_H = mean(mfa_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mfa_D = mean(mfa_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mfa_indep = mean(mfa_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mfa_sameparty = mean(mfa_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

mfa_pres_table_flipped = mfa_pres_table[c(2,1,3),]  |> t() 



# Ministers of Foreign Affairs, parliamentary system only

mfa_parl_table = wcy |>
  filter(!is.na(mfa_hawkdove) 
         & leader_hawkdove %in% c('C','D','H')
         & maj_parl==0
         # if you want to restrict to democracies:
         #  & dem ==1
         & leader_pm==1
  ) |>
  group_by(leader_hawkdove ) |> 
  summarise(
    mfa_H = mean(mfa_hawkdove=='H', na.rm=T) |> round(2) %>% `*`(100),
    mfa_D = mean(mfa_hawkdove=='D', na.rm=T) |> round(2) %>% `*`(100),
    mfa_indep = mean(mfa_hawkdove=='I', na.rm=T) |> round(2) %>% `*`(100),
    mfa_sameparty = mean(mfa_sameparty, na.rm=T) |> round(2) %>% `*`(100),
    n = n()
  )

mfa_parl_table_flipped = mfa_parl_table[c(2,1,3),]   |> t() 


# writing out MinForAff Pres/Parl table

cbind(mfa_pres_table_flipped, mfa_parl_table_flipped) |> 
  xtable() |> 
  print(file='tables/mfa_system_table.tex')




